<?php

/**
 * ECSHOP 商品分类
 * ============================================================================
 * * 版权所有 2005-2012 上海商派网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: liubo $
 * $Id: category.php 17217 2011-01-19 06:29:08Z liubo $
*/

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');

if ((DEBUG_MODE & 2) != 2)
{
    $smarty->caching = true;
}

/*------------------------------------------------------ */
//-- INPUT
/*------------------------------------------------------ */


/* 初始化分页信息 */
$page = isset($_REQUEST['page'])   && intval($_REQUEST['page'])  > 0 ? intval($_REQUEST['page'])  : 1;
$size = isset($_CFG['page_size'])  && intval($_CFG['page_size']) > 0 ? intval($_CFG['page_size']) : 12;
$bb_id  = isset($_REQUEST['bb_id']) && intval($_REQUEST['bb_id']) > 0 ? intval($_REQUEST['bb_id']) : 0;//活动类型ID
$cat_id  = isset($_REQUEST['cat_id']) && intval($_REQUEST['cat_id']) > 0 ? intval($_REQUEST['cat_id']) : 0;//所属行业ID

$sort  = (isset($_REQUEST['sort'])  && in_array(trim(strtolower($_REQUEST['sort'])), array('act_count', 'act_num'))) ? trim($_REQUEST['sort'])  : 'act_count';
$order = (isset($_REQUEST['order']) && in_array(trim(strtoupper($_REQUEST['order'])), array('ASC', 'DESC')))? trim($_REQUEST['order']) : 'DESC';

assign_template('c');
$count = get_goods_aa_count($bb_id,$cat_id);
$goods_aa_list = get_goods_aa_list( $size , $page , $bb_id , $cat_id, $sort , $order );
$smarty->assign('goods_aa_list', $goods_aa_list );
assign_pager2('goods_aa',   $cat_id, $count, $size, $sort, $order, $page, $keyword , $des_id, $act_id, $bb_id, $cid ); // 分页
assign_dynamic('goods_aa'); // 动态内容

$smarty->assign('categories',      get_categories_tree2()); //所属行业分类
$smarty->assign('cat_id',         $cat_id);  //所属行业ID
$smarty->assign('activity_list', get_goods_bbs(2 , $bb_id, true));//活动类型

$smarty->assign('bb_id', $bb_id);

$smarty->assign('menu_index',		'2');//当前导航标识
$smarty->display('goods_aa.dwt');


/**
 * 获得套装列表
 *
 * @access  public
 * @param   string  $children
 * @return  array
 */
function get_goods_aa_list( $size = 10, $page = 1 , $bb_id = 0, $cat_id = 0, $sort = 'act_id', $order="DESC")
{
    /* 获得商品列表 */
	$where = '';
	if($bb_id>0){
		$where .= ' AND act_type = '.$bb_id;
	}
	if($cat_id>0){
		$where .= ' AND cat2_id = '.$cat_id;
	}
    $sql = 'SELECT * FROM ' . $GLOBALS['ecs']->table('goods_aa') . " WHERE 1=1 $where ORDER BY $sort $order ";
    $res = $GLOBALS['db']->selectLimit($sql, $size, ($page - 1) * $size);

    $arr = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
		$arr[$row['act_id']]['act_name']		= $row['act_id'];
        $arr[$row['act_id']]['act_name']		= $row['act_name'];
        $arr[$row['act_id']]['act_desc']		= $row['act_desc'];
        $arr[$row['act_id']]['act_time']		= $row['act_time'];
		$arr[$row['act_id']]['act_format_time'] = local_date($GLOBALS['_CFG']['date_format'], $row['act_time']);
        $arr[$row['act_id']]['url']				= 'goods2.php?act_id='.$row['act_id'].'&t=1';
		$arr[$row['act_id']]['goods2_list']     = get_goods_aa_goods($row['act_id']);
    }

    return $arr;
}

function get_goods_aa_count( $bb_id = 0 , $cat_id = 0 ){
	$where = '';
	if($bb_id>0){
		$where .= ' AND act_type = '.$bb_id;
	}
	if($cat_id>0){
		$where .= ' AND cat2_id = '.$cat_id;
	}
    $sql = 'SELECT count(act_id) FROM ' . $GLOBALS['ecs']->table('goods_aa') . " WHERE 1=1 $where";
    return $GLOBALS['db']->getOne($sql);
}



?>
